﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OracleClient;
namespace AcctrueWMS.Foundation.DAL
{
    public static class WMSqlExcuter
    {
        static WMSqlInterpreter interpreter = new WMSqlInterpreter();
        #region 执行wmsql，须经解释器解释
        public static int ExecuteNonQuery(string commandText)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealNonQuery(cmdText);           
        }

        public static int ExecuteNonQuery(string connString,string commandText)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealNonQuery(cmdText);
        }
        public static int ExecuteNonQuery(string commandText, CommandType commandType)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealNonQuery(cmdText, commandType);
        }


        public static int ExecuteNonQuery( string commandText, CommandType commandType, params IDataParameter[] parameters)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealNonQuery(cmdText, commandType, parameters);
        }

        public static int ExecuteNonQuery(IDbConnection conn, string commandText,CommandType commandType, params IDataParameter[] parameters)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealNonQuery(conn,cmdText,commandType,parameters);
        }

        public static int ExecuteNonQuery(IDbTransaction tran, string commandText, CommandType commandType)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealNonQuery(tran,commandText,commandType);
        }

        public static int ExecuteNonQuery(IDbTransaction tran, string commandText, CommandType commandType, params IDataParameter[] parameters)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealNonQuery(tran,cmdText,commandType, parameters);
        }

        public static object ExecuteScalar(string commandText)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealScalar(cmdText);
        }

        public static object ExecuteScalar(string connString,string commandText)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealScalar(cmdText, CommandType.Text);
        }
        public static object ExecuteScalar(string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealScalar(cmdText, commandType,parameters);
        }

        //public static object ExecuteScalar(System.Data.IDbConnection conn, string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        //{
        //    string cmdText = interpreter.GetRealSql(commandText);
        //    return ExecuteRealScalar(conn, cmdText, commandType, parameters);
        //}

        public static object ExecuteScalar(System.Data.IDbTransaction tran, string commandText, System.Data.CommandType commandType)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealScalar(tran, cmdText, commandType);
        }

        public static object ExecuteScalar(System.Data.IDbTransaction tran, string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealScalar(tran, cmdText, commandType, parameters);
        }

        public static DataTable ExecuteDataTable(string commandText)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealDataTable(cmdText);
        }

        public static DataTable ExecuteDataTable(string commandText, System.Data.CommandType commandType)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealDataTable(cmdText, commandType);
        }

        public static DataTable ExecuteDataTable(string commandText, params System.Data.IDataParameter[] parameters)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealDataTable(cmdText, parameters);
        }

        public static DataTable ExecuteDataTable(string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealDataTable(cmdText, commandType, parameters);
        }

        //public static DataTable ExecuteDataTable(System.Data.IDbConnection conn, string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        //{
        //    string cmdText = interpreter.GetRealSql(commandText);
        //    return ExecuteRealDataTable(conn, cmdText, commandType, parameters);
        //}

        public static DataTable ExecuteDataTable(System.Data.IDbTransaction tran, string commandText, System.Data.CommandType commandType)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealDataTable(tran, cmdText, commandType);
        }

        public static DataTable ExecuteDataTable(System.Data.IDbTransaction tran, string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        {
            string cmdText = interpreter.GetRealSql(commandText);
            return ExecuteRealDataTable(tran, cmdText, commandType, parameters);
        }      
        
        #endregion

        #region 执行真正的SQL

        public static int ExecuteRealNonQuery(string commandText)
        {
            return ExecuteRealNonQuery(commandText, System.Data.CommandType.Text);   //默认为text
        }

        public static int ExecuteRealNonQuery(string commandText, System.Data.CommandType commandType)
        {
           return ExecuteRealNonQuery(commandText,commandType,null);
        }

        public static int ExecuteRealNonQuery(string commandText, params System.Data.IDataParameter[] parameters)
        {
            return ExecuteRealNonQuery(commandText, System.Data.CommandType.Text, parameters);
        }

        public static int ExecuteRealNonQuery(string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        {
            switch (DBFactory.DbType)
            {
                case DBTypeConst.ORACLE:
                    return OracleHelper.ExecuteNonQuery(DBFactory.ConnString,commandType,commandText, DBFactory.ToRealParamters<OracleParameter>(parameters));
                case DBTypeConst.MSSQL:
                    return SqlHelper.ExecuteNonQuery(DBFactory.ConnString, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>(parameters) );
                default:
                    return SqlHelper.ExecuteNonQuery(DBFactory.ConnString, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>(parameters));
            }            
        }

        public static int ExecuteRealNonQuery(IDbConnection conn, string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        {
            switch (DBFactory.DbType)
            {
                case DBTypeConst.ORACLE:
                    return OracleHelper.ExecuteNonQuery(conn as OracleConnection, commandType, commandText, DBFactory.ToRealParamters<OracleParameter>( parameters));
                case DBTypeConst.MSSQL:
                    return SqlHelper.ExecuteNonQuery(conn as SqlConnection, commandType, commandText,DBFactory.ToRealParamters<SqlParameter>( parameters));
                default:
                    return SqlHelper.ExecuteNonQuery(conn as SqlConnection, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>( parameters) );
            }
        }

        public static int ExecuteRealNonQuery(System.Data.IDbTransaction tran, string commandText, System.Data.CommandType commandType)
        {
            return ExecuteRealNonQuery(tran, commandText, commandType, null);
        }


        public static int ExecuteRealNonQuery(System.Data.IDbTransaction tran, string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        {
            switch (DBFactory.DbType)
            {
                case DBTypeConst.ORACLE:
                    return OracleHelper.ExecuteNonQuery(tran as OracleTransaction, commandType, commandText, DBFactory.ToRealParamters<OracleParameter>( parameters));
                case DBTypeConst.MSSQL:
                    return SqlHelper.ExecuteNonQuery(tran as SqlTransaction, commandType, commandText,DBFactory.ToRealParamters<SqlParameter>( parameters));
                default:
                    return SqlHelper.ExecuteNonQuery(tran as SqlTransaction, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>( parameters) );
            }            
        }

        public static object ExecuteRealScalar(string commandText)
        {
            return ExecuteRealScalar(commandText,System.Data.CommandType.Text);
        }

        public static object ExecuteRealScalar(string commandText, System.Data.CommandType commandType)
        {
            return ExecuteRealScalar(commandText, commandType, null);
        }

        public static object ExecuteRealScalar(string commandText, params System.Data.IDataParameter[] parameters)
        {
            return ExecuteRealScalar(commandText, System.Data.CommandType.Text, parameters);
        }

        public static object ExecuteRealScalar(string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        {
            switch (DBFactory.DbType)
            {
                case DBTypeConst.ORACLE:
                    return OracleHelper.ExecuteScalar(DBFactory.ConnString, commandType, commandText, DBFactory.ToRealParamters<OracleParameter>( parameters));
                case DBTypeConst.MSSQL:
                    return SqlHelper.ExecuteScalar(DBFactory.ConnString, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>( parameters));
                default:
                    return SqlHelper.ExecuteScalar(DBFactory.ConnString, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>( parameters));
            }    

        //    return ExecuteRealScalar(DBFactory.NewConnection(), commandText, commandType, parameters);
        }

        //public static object ExecuteRealScalar(System.Data.IDbConnection conn, string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        //{
        //    switch (DBFactory.DbType)
        //    {
        //        case DBTypeConst.ORACLE:
        //            return OracleHelper.ExecuteScalar(conn as OracleConnection, commandType, commandText, DBFactory.ToRealParamters<OracleParameter>( parameters));
        //        case DBTypeConst.MSSQL:
        //            return SqlHelper.ExecuteScalar(conn as SqlConnection, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>( parameters));
        //        default:
        //            return SqlHelper.ExecuteScalar(conn as SqlConnection, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>( parameters));
        //    }    
        //}



        public static object ExecuteRealScalar(System.Data.IDbTransaction tran, string commandText, System.Data.CommandType commandType)
        {
            return ExecuteRealScalar(tran, commandText, commandType, null);
        }

        public static object ExecuteRealScalar(System.Data.IDbTransaction tran, string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        {
            switch (DBFactory.DbType)
            {
                case DBTypeConst.ORACLE:
                    return OracleHelper.ExecuteScalar(tran as OracleTransaction, commandType, commandText, DBFactory.ToRealParamters<OracleParameter>( parameters));
                case DBTypeConst.MSSQL:
                    return SqlHelper.ExecuteScalar(tran as SqlTransaction, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>( parameters));
                default:
                    return SqlHelper.ExecuteScalar(tran as SqlTransaction, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>( parameters));
            }            
        }



        public static DataTable ExecuteRealDataTable(string commandText)
        {
            return ExecuteRealDataTable(commandText, System.Data.CommandType.Text);
        }

        public static DataTable ExecuteRealDataTable(string commandText, System.Data.CommandType commandType)
        {
            return ExecuteRealDataTable(commandText, commandType,null);
        }

        public static DataTable ExecuteRealDataTable(string commandText, params System.Data.IDataParameter[] parameters)
        {
            return ExecuteRealDataTable(commandText, System.Data.CommandType.Text, parameters);
        }

        public static DataTable ExecuteRealDataTable(string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        {
            switch (DBFactory.DbType)
            {
                case DBTypeConst.ORACLE:
                    return OracleHelper.ExecuteDataset(DBFactory.ConnString, commandType, commandText, DBFactory.ToRealParamters<OracleParameter>( parameters)).Tables[0];
                case DBTypeConst.MSSQL:
                    return SqlHelper.ExecuteDataset(DBFactory.ConnString, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>(parameters)).Tables[0];
                default:
                    return SqlHelper.ExecuteDataset(DBFactory.ConnString, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>(parameters)).Tables[0];
            }   
        }

        //public static DataTable ExecuteRealDataTable(System.Data.IDbConnection conn, string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        //{
        //    return DBFactory.CreateInstance().ExecuteDataTable(conn,commandText, commandType, parameters);
        //}



        public static DataTable ExecuteRealDataTable(System.Data.IDbTransaction tran, string commandText, System.Data.CommandType commandType)
        {
            return ExecuteRealDataTable(tran, commandText, commandType, null);
        }

        public static DataTable ExecuteRealDataTable(System.Data.IDbTransaction tran, string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        {
            switch (DBFactory.DbType)
            {
                case DBTypeConst.ORACLE:
                    return OracleHelper.ExecuteDataset(tran as OracleTransaction, commandType, commandText, DBFactory.ToRealParamters<OracleParameter>(parameters)).Tables[0];
                case DBTypeConst.MSSQL:
                    return SqlHelper.ExecuteDataset(tran as SqlTransaction, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>(parameters)).Tables[0];
                default:
                    return SqlHelper.ExecuteDataset(tran as SqlTransaction, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>(parameters)).Tables[0];
            } 
        }












        //-----------------

        public static IDataReader ExecuteRealDataReader(string commandText)
        {
            return ExecuteRealDataReader(commandText, System.Data.CommandType.Text);
        }

        public static IDataReader ExecuteRealDataReader(string commandText, System.Data.CommandType commandType)
        {
            return ExecuteRealDataReader(commandText, commandType, null);
        }

        public static IDataReader ExecuteRealDataReader(string commandText, params System.Data.IDataParameter[] parameters)
        {
            return ExecuteRealDataReader(commandText, System.Data.CommandType.Text, parameters);
        }

        public static IDataReader ExecuteRealDataReader(string commandText, System.Data.CommandType commandType, params System.Data.IDataParameter[] parameters)
        {
            switch (DBFactory.DbType)
            {
                case DBTypeConst.ORACLE:
                    return OracleHelper.ExecuteReader(DBFactory.ConnString, commandType, commandText, DBFactory.ToRealParamters<OracleParameter>(parameters));
                case DBTypeConst.MSSQL:
                    return SqlHelper.ExecuteReader(DBFactory.ConnString, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>(parameters));
                default:
                    return SqlHelper.ExecuteReader(DBFactory.ConnString, commandType, commandText, DBFactory.ToRealParamters<SqlParameter>(parameters));
            }
        }

        #endregion

       
    }
}
